package com.dhl.fin.api.service;

import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.domain.Stock;
import com.dhl.fin.api.enums.BasicAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by CuiJianbo on 2020.05.17.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StockServiceImpl extends CommonService<Stock> {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<Map> getSelectLine() {
        String[] lineNames = WebUtil.getStringArrayParam("basicAspect");
        List<Map> result = new LinkedList<>();
        for (String lineName : lineNames) {
//            String sql = String.format("select format(year_month,'yyyy-MM-dd') name, %s value from debt_load where  %s is not null order by year_month", lineName, lineName);
            String sql = String.format("select  year_month name, %s value from debt_load where  %s is not null order by year_month", lineName, lineName);
            List datas = jdbcTemplate.queryForList(sql);

            String name = BasicAspect.valueOf(lineName.toUpperCase()).getName();
            result.add(MapUtil.builder().add("name", name).add("series", datas).build());
        }

        return result;

    }

    public Map totalMarketValue() {
        String sql = "select date_format(date,'%Y-%m-%d') name,  round(sum(a.total_market_value) * 1.0/ 1000, 2) value from t_stock a where remove = 0 group by a.date order by a.date";
        List datas = jdbcTemplate.queryForList(sql);

        return MapUtil.builder().add("name", "总市值").add("series", datas).build();
    }

    public void freshTotalMarketValue() {

        jdbcTemplate.execute("truncate table t_stock_total_market_value");

        String sql = "select date  , round(sum(a.total_market_value) * 1.0 / 1000, 2) marketValue" + " from t_stock a where remove = 0 group by a.date";
        List<Map<String, Object>> datas = jdbcTemplate.queryForList(sql);
        datas.forEach(p -> {
            String date = MapUtil.getString(p, "date");
            String marketValue = MapUtil.getString(p, "marketValue");
            String insertSql = String.format("insert into t_stock_total_market_value(point_date, market_value) values('%s',%s)", date, marketValue);
            jdbcTemplate.execute(insertSql);
        });
    }


    /**
     * 添加新的每日总市值到表里
     */
    public void addDateValue(String date) {
        String sql = "select round(sum(a.total_market_value) * 1.0 / 1000, 2) totalValue from t_stock a where remove = 0 and date = '" + date + "'";

        Object totalValue = jdbcTemplate.queryForMap(sql).get("totalValue");
        if (ObjectUtil.notNull(totalValue)) {
            String insertSql = String.format("insert into t_stock_total_market_value(point_date, market_value) values('%s',%s)", date, totalValue);
            jdbcTemplate.execute(insertSql);
        }


    }

}
